package com.rslai.base.tool.servertest.transport.command;

import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.transport.http.HttpService;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;

public class SHttpExecuteCommand extends HttpExecuteCommand {
    public SHttpExecuteCommand(String id, String url, String method, String desc) {
        super(id, url, method, desc);
    }

    public Response execute(List<KeyValueStore> params) {
        Map headers = getHttpHeaders(params);
        this.params = removedHttpHeaders(params);

        if (logger.isInfoEnabled()) {
            logger.info("Http request start: url={}, method={}, headers={}, params={}", new Object[]{this.url, this.method, headers, getParamsAsString(this.params)});
        }
        HttpService.setHeaders(headers);

        if (isEntityRequest(this.method)) {
            this.method = fixMethod(this.method);
            return HttpService.entityRequestWithStream(this.url, this.method, this.params);
        }

        Response response = HttpService.getWithStream(this.url, this.params);

        if (logger.isInfoEnabled()) {
            logger.info("Http Execute : method={}, url={}, params={}, response={}", new Object[]{this.method, this.url, getParamsAsString(params), response});
        }

        return response;
    }
}
